package com.gwd.task.services.impl;

import cn.hutool.core.bean.BeanUtil;
import com.gwd.task.entity.AttachmentEntity;
import com.gwd.task.model.AttachmentDTO;
import com.gwd.task.repository.AttachmentRepository;
import com.gwd.task.services.AttachmentServer;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhiyongming
 * @date 2020/12/14 10:55
 */
@Service
public class AttachmentServerImpl implements AttachmentServer {

    private static final Integer TEST = 2;
    private static final Integer SELF = 1;
    //private static final Integer BUG = 0;

    @Resource
    private AttachmentRepository attachmentRepository;

    @Override
    public List<AttachmentDTO> findByCode(String code) {
        return null;
    }

    @Override
    public List<AttachmentDTO> findByCodeList(List<String> codes) {
        return attachmentRepository.findByCodeIn(codes).stream().map(AttachmentDTO::new).collect(Collectors.toList());
    }

    @Override
    public List<AttachmentDTO> findByNameList(List<String> names) {
        return attachmentRepository.findByFileNameIn(names).stream().map(AttachmentDTO::new).collect(Collectors.toList());
    }

    @Override
    public AttachmentDTO findByFileName(String fileName) {
        AttachmentEntity entity = attachmentRepository.findByFileName(fileName);
        if (entity!=null) {
            return new AttachmentDTO(entity);
        }
        return null;
    }

    @Override
    public void save(AttachmentDTO dto) {
        AttachmentEntity entity = new AttachmentEntity();
        BeanUtil.copyProperties(dto, entity);
        attachmentRepository.save(entity);
    }

    @Override
    public void bindCode(List<AttachmentDTO> dtoList, String code) {
        //绑定已更新的
        List<String> fileNameList = dtoList.stream().filter(a -> a.getType()==1).map(AttachmentDTO::getFileName).collect(Collectors.toList());
        attachmentRepository.bindCode(code, fileNameList);
        //标记删除
        List<Long> ids = dtoList.stream().filter(a -> a.getType()==2).map(AttachmentDTO::getId).collect(Collectors.toList());
        attachmentRepository.deleteByIdIn(ids);
    }

    @Override
    public void bindCode(String tempCode, String code) {
        List<AttachmentEntity> list = findByTempCode(tempCode);
        if (list.size()==0) return;
        attachmentRepository.saveAll(list.stream().peek(e->{
            e.setCode(code);
            e.setAtType(TEST);
        }).collect(Collectors.toList()));
    }

    @Override
    public void self(String tempCode, String code) {
        List<AttachmentEntity> list = findByTempCode(tempCode);
        if (list.size()==0) return;
        attachmentRepository.saveAll(list.stream().peek(e->{
            e.setCode(code);
            e.setAtType(SELF);
        }).collect(Collectors.toList()));
    }

    @Override
    public void remove(String fileName) {

    }

    private List<AttachmentEntity> findByTempCode(String tempCode) {
        return attachmentRepository.findByTempCode(tempCode);
    }

}
